﻿// <copyright file="BinarySerializationTests.cs" company="Sinbadsoft">
//        Copyright (c) Sinbadsoft 2009.
//        This file is released under the terms of the
//        GNU Library General Public License (LGPL) version 2.1
//        Please refer to the "License.txt" accompanying this file.
// </copyright>
// <author>Chaker NAKHLI</author>
// <email>Chaker.Nakhli@Sinbadsoft.com</email>
// <date>2009/04/07</date>

namespace Sinbadsoft.AdvancedCollections.Tests.ReversibleDictionary
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    using MbUnit.Framework;

    /// <summary> Test class for <see cref="ReversibleDictionary{TKey,TValue}"/> 
    /// and <see cref="SortedReversibleDictionary{TKey,TValue}"/>
    /// binary serialization. </summary>
    [TestFixture]
    public class BinarySerializationTests
    {
        private IEnumerable<KeyValuePair<int, string>> data;

        [SetUp]
        public void SetUp()
        {
            List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
            for (int i = 0; i < 10; ++i)
            {
                list.Add(new KeyValuePair<int, string>(i, "S" + i));
            }

            this.data = list;
        }

        [Test]
        public void SerializeReversibleDictionary()
        {
            ReversibleDictionary<int, string> rdict =
                new ReversibleDictionary<int, string>(this.data, new IntEqComparer(27), null);

            ReversibleDictionary<int, string> result = 
                CommonTests<int, string>.InMemorySerializeDeserialize(rdict);

            Assert.AreEqual(rdict.KeyComparer, result.KeyComparer);
            Assert.AreEqual(rdict.ValueComparer, result.ValueComparer);
        }

        [Test]
        public void SerializeSortedReversibleDictionary()
        {
            SortedReversibleDictionary<int, string> sortedrdict =
                new SortedReversibleDictionary<int, string>(this.data, new IntEqComparer(17), null);

            SortedReversibleDictionary<int, string> result = 
                CommonTests<int, string>.InMemorySerializeDeserialize(sortedrdict);

            Assert.AreEqual(sortedrdict.KeyComparer, result.KeyComparer);
            Assert.AreEqual(sortedrdict.ValueComparer, result.ValueComparer);
        }

        [Serializable]
        private struct IntEqComparer : IEqualityComparer<int>, IComparer<int>
        {
            public int Quotient;

            public IntEqComparer(int quotient)
            {
                this.Quotient = quotient;
            }

            public bool Equals(int x, int y)
            {
                return x % this.Quotient == y % this.Quotient;
            }

            public int GetHashCode(int obj)
            {
                return obj % this.Quotient;
            }

            public int Compare(int x, int y)
            {
                return (x % this.Quotient) - (y % this.Quotient);
            }
        }
    }
}
